package cn.tedu.serviceEdu.controller;


import cn.tedu.commonUtils.R;
import cn.tedu.serviceBase.exception.EducationException;
import cn.tedu.serviceEdu.entity.EduChapter;
import cn.tedu.serviceEdu.entity.EduVideo;
import cn.tedu.serviceEdu.service.EduChapterService;
import cn.tedu.serviceEdu.service.EduCourseService;
import cn.tedu.serviceEdu.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author hong
 * @since 2023-05-18
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
@Api(tags = "课程章节Api")
public class EduChapterController {
    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private EduVideoService videoService;

    @ApiOperation("查询课程章节")
    @GetMapping("/getChapter/{courseId}")
    public R getChapter(@PathVariable String courseId){
        List<EduChapter> chapterList =  chapterService.getChapterList(courseId);
        return R.ok().data("items",chapterList);
    }

    @ApiOperation("删除课程章节")
    @DeleteMapping("/deleteChapter/{id}")
    public R deleteChapter(@PathVariable String id){
        if(id.isEmpty()){
            throw new EducationException(20001,"课程章节不存在");
        }
        LambdaQueryWrapper<EduVideo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduVideo::getChapterId,id);
        int count = videoService.count(wrapper);
        if(count > 0){
            return R.error().msg("该分章节下存在视频课程，请先删除视频课程");
        }
        boolean flag = chapterService.removeById(id);
        if (!flag) {
            throw new EducationException(20001,"课程章节山吃失败");
        }
        return R.ok();
    }

    @ApiOperation("添加课程章节")
    @PostMapping("/saveChapter")
    public R saveChapter(@RequestBody EduChapter chapter){
        if(chapter == null){
            throw new EducationException(20001,"课程章节不存在");
        }
        boolean save = chapterService.save(chapter);
        if (!save) {
            throw new EducationException(20001,"课程章节删除失败");
        }
        return R.ok();
    }

    @ApiOperation("id查询课程章节")
    @GetMapping("/getChapterById/{id}")
    public R getChapterById(@PathVariable String id){
        if(id.isEmpty()){
            throw new EducationException(20001,"课程章节不存在");
        }
        EduChapter chapter = chapterService.getById(id);
        return R.ok().data("items",chapter);
    }

    @ApiOperation("修改课程章节")
    @PutMapping("/updateChapter")
    public R getChapter(@RequestBody EduChapter chapter){
        if(chapter == null){
            throw new EducationException(20001,"课程章节不存在");
        }
        boolean update = chapterService.updateById(chapter);
        if(!update){
            throw new EducationException(20001,"课程章节修改失败");
        }
        return R.ok();
    }

}

